package org.gd.jms.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQSender implements Runnable {

    private String QUEUE_NAME;
    private ConnectionFactory factory;
    private static final long messCount = 1000;

    public RabbitMQSender() {
        factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        factory.setPort(5672);
    }

    public RabbitMQSender(String QUEUE_NAME) {
        this();
        this.QUEUE_NAME = QUEUE_NAME;
    }

    @SuppressWarnings("unused")
    public String getQUEUE_NAME() {
        return QUEUE_NAME;
    }

    @Override
    public void run() {
        try {
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            int count = 0;
            while (count < messCount) {
                String message = "Hello World! " + count++;
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            }
            //System.out.println(" [" + this.getClass().getSimpleName() + "] Sent '" + message + "'");
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(0);
        } finally {
            System.out.println("Goodbye RabbitMQ Sender Client!");
        }
    }
}
